<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/1/29 14:22
 * Remark:
 */

namespace App\Http\Middleware\Auth;

use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\AdminTokenFunc;
use App\Func\ResponseFunc;
use App\Func\TokenFuc;
use Closure;
use Illuminate\Http\Exceptions\HttpResponseException;

class RequestMiddleware
{

    private function gl($permitPath,$request){
        foreach($permitPath as $value){
            if(strstr($request->path(),$value)){
                return true;
            }
        }
        return false;
    }

    public function handle($request, Closure $next)
    {
        // 允许通过的路由
        $permitPath = ['apx/login'];
        if(!($this->gl($permitPath,$request))){
            $adminLoginInfo = new AdminTokenFunc();
            if(isset($adminLoginInfo['error'])){
                throw new HttpResponseException( ResponseFunc::returnJson(
                    'error',
                    null,
                    "登录失效".$adminLoginInfo['error'],
                    401,
                ));
            }
        }
        return $next($request);
    }
}